
import bean.Barang;
import bean.Konsumen;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SimpanDataKonsumen extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String noKirim = request.getParameter("nomor");
        String noBarang = request.getParameter("no");
        String namaKirim = request.getParameter("nama");
        String alamatKirim = request.getParameter("alamat");
        String noTelpKirim = request.getParameter("telp");
        String namaTerima = request.getParameter("terima");
        String alamatTerima = request.getParameter("alamatterima");
        String noTelpTerima = request.getParameter("telpterima");

        Konsumen konsumen = new Konsumen();
        konsumen.setNoPengirim(noKirim);
        konsumen.setNoBarang(noBarang);
        konsumen.setNamaPengirim(namaKirim);
        konsumen.setAlamatPengirim(alamatKirim);
        konsumen.setNoTelpPengirim(noTelpKirim);
        konsumen.setNamaPenerima(namaTerima);
        konsumen.setAlamatPenerima(alamatTerima);
        konsumen.setNoTelpPenerima(noTelpTerima);

//        SimpanDataBarang sdb = new SimpanDataBarang();
//        sdb.processRequest(request, response);

        try {
            Connection conn = koneksi.Koneksi.open();
            Statement state = conn.createStatement();
            String query = "insert into datakonsumen (no_pelanggan, no_barang, nama_pengirim, notelp_pengirim, alamat_pengirim, "
                    + "nama_penerima, notelp_penerima, alamat_penerima) "
                    + "values ('" + konsumen.getNoPengirim() + "','"
                    + konsumen.getNoBarang() + "','"
                    + konsumen.getNamaPengirim() + "','"
                    + konsumen.getNoTelpPengirim() + "','"
                    + konsumen.getAlamatPengirim() + "','"
                    + konsumen.getNamaPenerima() + "','"
                    + konsumen.getNoTelpPenerima() + "','"
                    + konsumen.getAlamatPenerima() + "')";
//                    + "where data_barang.no_barang =datakonsumen.no_barang";
            state.executeUpdate(query);
            RequestDispatcher req = request.getRequestDispatcher("FrameUtama.jsp");
            req.forward(request, response);
            out.close();
        } catch (SQLException ex) {
            Logger.getLogger(SimpanDataKonsumen.class.getName()).log(Level.SEVERE, null, ex);
        }

//        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
